<?php
    if(ofAuthenticated()){
        $memID=$_GET["memID"];
    }
    if($memID!=""){
        require_once dirname(__FILE__)."/../class/member.class.php";
        $memberObj=new member($memID);
        $member=$memberObj->getMember();
    }
?>
<script type="text/javascript">
    var geoLocation;
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
            $('#geoLocation').val( "Geolocation is not supported by this browser.");
        }
    }

    function showPosition(position) {
        $('#geoLocation').val(position.coords.latitude + "," + position.coords.longitude);
        geoLocation=position.coords.latitude + "," + position.coords.longitude;
        $('#map-canvas').show();
        setMap();
    }
    var marker;
    var map;

    var myLatlng = new google.maps.LatLng(geoLocation);

    function initialize() {

        var mapOptions = {
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: myLatlng
            };
        map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

        marker = new google.maps.Marker({
            map:map,
            draggable:true,
            animation: google.maps.Animation.DROP,
            position: myLatlng
        });

        var markerIcon = {
            url: 'img/markerMedium.png'
        };

        marker.setIcon(markerIcon);


        //google.maps.event.addListener(marker, 'click', toggleBounce);
        google.maps.event.addListener (marker, 'drag', function (event) {
            document.getElementById("geoLocation").value = marker.getPosition().lat()+","+marker.getPosition().lng();
            
        });

        google.maps.event.addListener (marker, 'dragend', function (event) {
            getProvince();
            var point = marker.getPoint();
            map.panTo(point);
        });
        
    }
    
    function getProvince(){
        lat=marker.getPosition().lat();
        lng=marker.getPosition().lng();
        $.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lng+'&sensor=false'
        ,function(data){
            var province=data["results"][0]["address_components"][3]["short_name"];
        });
    }

    function toggleBounce() {
        if (marker.getAnimation() != null){
            marker.setAnimation(null);
        }else{
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);

    function setMap(){
        var temp=geoLocation.split(",");
        myLatlng = new google.maps.LatLng(temp[0],temp[1]);
        initialize();
    }
    
    function formSubmit(){
        var cardID = $('#cardID');
        var title = $('#title');
        var firstName = $('#firstName');
        var lastName = $('#lastName');
        var mobile = $('#mobile');
        var geoLocation = $('#geoLocation');
        var coopID = $('#coopID');
        
        if(cardID.val()==''){
            alert('กรุณากรอก "หมายเลขบัตรประชาชน"');
            cardID.focus();
        }else if(title.val()==''){
            alert('กรุณากรอก "คำนำหน้าชื่อ"');
            title.focus();
        }else if(firstName.val()==''){
            alert('กรุณากรอก "ชื่อ"');
            firstName.focus();
        }else if(lastName.val()==''){
            alert('กรุณากรอก "นามสกุล"');
            lastName.focus();
        }else if(mobile.val()==''){
            alert('กรุณากรอก "มือถือ"');
            mobile.focus();
        }else if(geoLocation.val()==''){
            alert('กรุณาระบุ "พิกัดที่อยู่"');
            geoLocation.focus();
        }else{
            $('#memberRegistForm').submit()
        }
    }
        
</script>
<style type="text/css">
    #map-canvas {display: none;height: 6cm;width: 100%;background-color: #ccc;}
</style>
<div id="tempDIV"></div>
<div style="padding: 5px;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <?php
                if($memID==""){
                        ?>
            <span class='glyphicon glyphicon-edit'></span>&nbsp;ลงทะเบียนเกษตรกร
                        <?php
                }else{
                    ?>
            <span class='glyphicon glyphicon-cog'></span>&nbsp;แก้ไขข้อมูลเกษตรกร
                        <?php
                }
            ?>
        </div>
        <div class="panel-body">
            <form id="memberRegistForm" method="post" action="member/memberRegistSQL.php" class="form-horizontal" autocomplete="off">
                <?php
                    if($memID!=""){
                        ?>
                <input type="hidden" name="memID" value="<?=$memID?>">
                            <?php
                    }
                ?>
                <fieldset>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="title">หมายเลขบัตรประชาชน</label>  
                    <div class="col-md-4">
                        <input id="cardID" name="cardID" type="number" placeholder="หมายเลขบัตรประชาชน" class="form-control input-md" required="" autofocus="" maxlength="13"
                            value="<?=$member["cardID"]?>"
                            >
                        <span class="help-block">กรอกเฉพาะตัวเลขเท่านั้น</span>  
                    </div>
                </div>
                
                <?php
                    if(ofAuthenticated()){
                        ?>
                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="user">ชื่อผู้ใช้ระบบ</label>  
                    <div class="col-md-4">
                        <input id="user" name="user" type="text" placeholder="ชื่อผู้ใช้ระบบ" class="form-control input-md" required=""
                            value="<?=$member["user"]?>"
                            >
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="user">รหัสผ่าน</label>  
                    <div class="col-md-4">
                        <input id="pass" name="pass" type="password" placeholder="รหัสผ่าน" class="form-control input-md">
                    </div>
                </div>
                            <?php
                    }
                ?>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="title">คำนำหน้าชื่อ</label>  
                    <div class="col-md-4">
                        <input type="radio" name="title" value="นาย"
                            <?php
                                if($member["title"]=="นาย" || $member["title"]==""){
                                    echo "checked";
                                }
                            ?>
                            > นาย
                        &nbsp;&nbsp;&nbsp;
                        <input type="radio" name="title" value="นาง"
                            <?php
                                if($member["title"]=="นาง"){
                                    echo "checked";
                                }
                            ?>
                            > นาง
                        &nbsp;&nbsp;&nbsp;
                        <input type="radio" name="title" value="นางสาว"
                            <?php
                                if($member["title"]=="นางสาว"){
                                    echo "checked";
                                }
                            ?>
                            > นางสาว
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="firstName">ชื่อ</label>  
                    <div class="col-md-4">
                        <input id="firstName" name="firstName" type="text" placeholder="ชื่อ" class="form-control input-md" required=""
                            value="<?=$member["firstName"]?>"
                            >
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="lastName">นามสกุล</label>  
                    <div class="col-md-4">
                        <input id="lastName" name="lastName" type="text" placeholder="นามสกุล" class="form-control input-md" required=""
                            value="<?=$member["lastName"]?>"
                            >
                    </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="mobile">มือถือ</label>  
                    <div class="col-md-4">
                        <input id="mobile" name="mobile" type="text" placeholder="มือถือ" class="form-control input-md"
                            value="<?=$member["mobile"]?>"
                            >
                    </div>
                </div>

                <!-- Appended Input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="cardID">พิกัดที่อยู่</label>
                    <div class="col-md-4">
                        <div class="input-group">
                            <input id="geoLocation" name="geoLocation" class="form-control" placeholder="พิกัดที่อยู่" type="text"
                                value="<?=$member["geoLocation"]?>"
                                >
                            <span class="input-group-addon" onclick="getLocation()" style="cursor: pointer;">
                                <span class="glyphicon glyphicon-screenshot"></span>
                                พิกัดปัจจุบัน
                            </span>
                        </div>
                        <p class="help-block">เช่น 18.629098892,98.876014709</p>
                    </div>
                </div>

                <div id="memberMap">
                    <div id="map-canvas"></div>
                </div>

                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="coopID">สังกัด</label>
                    <div class="col-md-4">
                        <?php
                            $coop=new coop();
                            $coopArr=$coop->getCoopArr();
                        ?>
                        <select id="coopID" name="coopID" class="form-control">
                            <option value="">ไม่ระบุ</option>
                            <?php
                                foreach ($coopArr as $key => $value) {
                                    ?>
                            <option value="<?=$key?>"><?=$value?></option>
                                        <?php
                                }
                            ?>
                        </select>
                    </div>
                </div>
                
                <!-- Select Basic -->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="coopID">เปิดเผยข้อมูล</label>
                    <div class="col-md-4">
                        <input type="checkbox" name="private" value="0"
                            <?php
                                if(isset($member) && $member["private"]=="0"){
                                    echo "checked";
                                }
                            ?>
                            > ยินยอมให้เปิดเผยข้อมูล
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label" for=""></label>
                    <div class="col-md-4">
                        <button id="" name="" class="btn btn-primary" style="float: right;" value='บันทึก'>บันทึก</button>
                    </div>
                </div>

                </fieldset>
            </form>
        </div>
    </div>
</div>
<?php
    if($memID!=""){
        $geoLocation=  explode(",", $member["geoLocation"]);
        ?>
<script type="text/javascript">
    $('#coopID').val('<?=$member["coopID"]?>');
    $('#map-canvas').show();
    
    myLatlng = new google.maps.LatLng(<?=$geoLocation[0]?>,<?=$geoLocation[1]?>);
    initialize();
</script>
            <?php
    }
?>